import { ref,watch } from 'vue';
import { useTagsStore } from '@/stores/tag.js'
import { useUserInfoStore } from '@/stores/userInfo.js'
import { removeToken } from "@/utils/auth";
import { useAreaInfoStore } from '@/stores/area.js';
import { useDistInfoStore } from '@/stores/dict.js'
import { useMarketInfoStore } from '@/stores/marketing.js'
//路由跳回到本页面时执行的事件，一般用于刷新当前分页
export function cacheChangeHandle(callBack,name){
    const tagsStore  = useTagsStore();
    watch(tagsStore,(newval)=>{

        if(newval.isRefresh && (name === tagsStore.activeRouteName)){

            callBack && callBack();
            tagsStore.changeIsRefresh(false);
        }
    })
}
/** 
 *@name 关闭标签页
*/
export function closeTag(name){
    if((name??'') !== ''){
        sessionStorage.setItem('closeTagName',name);
    }
    const tagsStore  = useTagsStore();
    
    tagsStore.changeCloseTag();
}
/**
 * @name 清楚所有缓存信息
*/
export function clearAllCache(){
 
    const tagsStore  = useTagsStore();
    const userInfoStore  = useUserInfoStore();
    const useAreaStore = useAreaInfoStore()
    const useDistStore = useDistInfoStore()
    const useMarketStore = useMarketInfoStore()
    tagsStore.clearActiveRouteName();
    tagsStore.clearTags();
    userInfoStore.removeUserInfo();
    useDistStore.removeDistInfo();
    useDistStore.removeDistInfos(); //移除带权限的数据字典
    useAreaStore.removeAeraInfo(); //移除地区信息
    useAreaStore.removeAeraRolesInfo(); //移除权限地区信息
    useMarketStore.removeMarketIndex(); //移除索引信息
    removeToken();
}
